package com.cloudspace.jindun.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.manager.UserManager;
import com.cloudspace.jindun.model.User;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/*
登录页
 */
public class RegisterActivity extends CustomTitleBaseActivity {


    @Bind(R.id.register_et_id)
    EditText mRegisterEtId;
    @Bind(R.id.register_et_prass)
    EditText mRegisterEtPrass;
    @Bind(R.id.register_bt_showpass)
    ImageView mRegisterBtShowpass;
    @Bind(R.id.register_bt_showpass_up)
    Button mRegisterBtShowpassUp;
    @Bind(R.id.register_tv_forgetpass)
    TextView mRegisterTvForgetpass;
    @Bind(R.id.register_rl_showpass)
    RelativeLayout mRegisterRlShowpass;

    private boolean isshow = true;
    private String idEtText = "1";
    private String passEtText = "1";
    private String onehttpCode = "0001";
    private String userId = "1";
    private String mUserId;

    private ProgressDialog  dialog;

    private User mUser;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register, false);
        ButterKnife.bind(this);
//        mRegisterEtPrass.setText("666666");
    }

    private void requestData(String url, String userId, String userPassword) {
        OkHttpUtils.post()
                .url(url)
                .addParams("userId", userId)
                .addParams("userPassword", userPassword)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        dialog.dismiss();
                        ToastMaker.showToastShort(R.string.generic_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dialog.dismiss();
                        Gson gson = new Gson();
                        mUser = gson.fromJson(response, User.class);

                        if (mUser != null) {
                            int status = mUser.getStatus();
                            if (status == 200) {
                                initManager();
                                Intent intent = new Intent();
                                switch (mUser.getData().getUserStatus()) {
                                    case "0001":
                                        intent.setClass(RegisterActivity.this, WdActivity.class);
                                        startActivity(intent);
                                        finish();
                                        break;
                                    case "0002":
                                        intent.setClass(RegisterActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                        break;
                                    default:
                                        ToastMaker.showToastShort(R.string.generic_error);
                                        break;
                                }

                            } else {
                                ToastMaker.showToastShort(R.string.name_password_error);
                            }
                        } else {
                            ToastMaker.showToastShort(R.string.login_error);
                        }
                    }
                });
    }

    private void initManager() {
        if (ObjUtil.isEmpty(mUser) || ObjUtil.isEmpty(mUser.getData())){
            return;
        }

        UserManager.getInstance().addLoginUser(mUser.getData());
        JindunApp.getInstance().setUserInfo(User.UserData.generateDatabean(mUser.getData()));
    }

    @OnClick({R.id.register_rl_showpass, R.id.register_bt_showpass_up, R.id.register_tv_forgetpass})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_rl_showpass:
                if (isshow) {
                    mRegisterEtPrass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mRegisterBtShowpass.setImageResource(R.drawable.eye_02);
                    isshow = false;
                } else {
                    mRegisterEtPrass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mRegisterBtShowpass.setImageResource(R.drawable.eye_01);
                    isshow = true;
                }
                break;
            case R.id.register_bt_showpass_up:
                idEtText = mRegisterEtId.getText().toString();
                passEtText = mRegisterEtPrass.getText().toString();
                dialog = new ProgressDialog(this);
                dialog.setTitle(getResources().getString(R.string.login_now));  //
                dialog.show();
                requestData(YXHttpUrl.LOGIN_URI, idEtText, passEtText);

                break;
            case R.id.register_tv_forgetpass:
                Intent intent2 = new Intent();
                intent2.setClass(RegisterActivity.this, ForgetpassActivity.class);
                startActivity(intent2);
                break;
        }
    }

    public static void showActivity(Context context){
        Intent intent=new Intent();
        intent.setClass(context, RegisterActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

}
